import { type DramaPayRank, type HomeData, type UserPayRank, getHomeDataAxios } from '@/api/home';
import IconHandMoney from '~icons/solar/hand-money-linear';
import IconUsersGroupRounded from '~icons/solar/users-group-rounded-linear';
import IconCrownStar from '~icons/solar/crown-star-linear';
import IconUsersGroupTwoRounded from '~icons/solar/users-group-two-rounded-linear';
import IconUserHeart from '~icons/solar/user-heart-linear';
import IconEmojiFunnyCircle from '~icons/solar/emoji-funny-circle-linear';
export type PayRank = {
  /** 排名 */
  rank: number;
  /** 标题 */
  title: string;
  /** 子标题 */
  subTitle: string;
  /** 数量 */
  value: number | string;
};

export const getAxiosData = async () => {
  try {
    const response = await getHomeDataAxios();
    const data: HomeData = response.data;
    const weekNameList = (Array.isArray(data.weekNameList) ? response.data.weekNameList : []) as string[];

    // /** 销售与利润 */
    // const xsylrData: $Home.XsylrData = {
    //   xsdw: String(response.data.xsdw || ''),
    //   xsje: Number(response.data.xsje || '0'),
    //   xslrje: Number(response.data.xslrje || '0'),
    // };

    /** 付费分析 */
    const currWeekAmount = data.weekPayTotalAmount;
    const lastWeekAmount = data.lastWeekPayTotalAmount;
    const payAnalyse = {
      currWeekAmount: currWeekAmount >= 100000 ? currWeekAmount : Number(currWeekAmount.toFixed(2)),
      lastWeekAmount: lastWeekAmount >= 100000 ? lastWeekAmount : Number(lastWeekAmount.toFixed(2)),
      dataset: {
        series: [
          { name: '本周充值金额', data: data.thisWeekPayList },
          { name: '上周充值金额', data: data.lastWeekPayList },
        ],
        xAxis: { data: weekNameList },
      },
    };

    const userAnalyse = {
      currWeekVip: data.weekAddVipUserNum,
      currWeekUser: data.weekAddUserNum,
      vipDataset: [{ value: data.weekAddVipUserNum / data.vipUserCount, name: '会员新增率' }],
      userDataset: [{ value: data.weekAddUserNum / data.userCount, name: '用户新增率' }],
    };

    /** 追剧分析 */
    const currWeekChasingNum = data.thisWeekChasingNum;
    const lastWeekChasingNum = data.lastWeekChasingNum;
    const chasingAnalyse = {
      currWeekChasingNum: currWeekChasingNum >= 100000 ? currWeekChasingNum : Number(currWeekChasingNum.toFixed(2)),
      lastWeekChasingNum: lastWeekChasingNum >= 100000 ? lastWeekChasingNum : Number(lastWeekChasingNum.toFixed(2)),
      dataset: {
        series: [
          { name: '本周追剧人数', data: data.thisWeekChasingList },
          { name: '上周追剧人数', data: data.lastWeekChasingList },
        ],
        xAxis: { data: weekNameList },
      },
    };

    /** 消费分析 */
    const currWeekConsumption = data.thisWeekConsumeAmount;
    const lastWeekConsumption = data.lastWeekConsumeAmount;
    const consumptionAnalyse = {
      currWeekConsumption: currWeekConsumption >= 100000 ? currWeekConsumption : Number(currWeekConsumption.toFixed(2)),
      lastWeekConsumption: lastWeekConsumption >= 100000 ? lastWeekConsumption : Number(lastWeekConsumption.toFixed(2)),
      dataset: {
        series: [
          { name: '本周消费金额', data: data.thisWeekConsumeList },
          { name: '上周消费金额', data: data.lastWeekConsumeList },
        ],
        xAxis: { data: weekNameList },
      },
    };

    return Promise.resolve({
      /** 付费短剧排行 */
      dramaPayRankList: trimRankList(1, response.data.dramaPayRankList),
      /** 用户支付排行榜 */
      userPayRankList: trimRankList(2, response.data.userPayRankList),
      /** 费用统计 */
      payStatisticsList: [
        { name: '今日付费金额', color: 'var(--el-color-warning)', icon: IconHandMoney, numb: data.todayPayTotalAmount, suffix: '元' },
        { name: '今日付费用户', color: 'var(--el-color-success)', icon: IconUsersGroupRounded, numb: data.todayPayUserNum, suffix: '人' },
        { name: '本周付费金额', color: 'var(--el-color-danger)', icon: IconHandMoney, numb: data.weekPayTotalAmount, suffix: '元' },
        { name: '本周付费用户', color: 'var(--el-color-primary)', icon: IconUsersGroupRounded, numb: data.weekPayUserNum, suffix: '人' },
      ],
      /** 用户统计 */
      userStatisticsList: [
        { name: '用户总数', color: 'var(--el-color-primary)', icon: IconUsersGroupTwoRounded, numb: data.userCount, suffix: '人' },
        { name: '会员总数', color: 'var(--el-color-success)', icon: IconCrownStar, numb: data.vipUserCount, suffix: '人' },
        { name: '追剧人数', color: 'var(--el-color-danger)', icon: IconUserHeart, numb: data.dramaChasingUserNum, suffix: '人' },
        { name: '观看人数', color: 'var(--el-color-warning)', icon: IconEmojiFunnyCircle, numb: data.viewUserTotalNum, suffix: '人' },
      ],
      /** 本周付费分析 */
      payAnalyse,
      /** 用户/会员分析*/
      userAnalyse,
      /** 追剧分析 */
      chasingAnalyse,
      /** 消费分析 */
      consumptionAnalyse,
    });
  } catch (error) {
    return Promise.reject(error);
  }
};

/**
 * 整理排行榜数据
 */
function trimRankList<T extends 1 | 2>(type: T, list: T extends 1 ? DramaPayRank[] : UserPayRank[]): PayRank[] {
  if (type === 1) {
    return (list as DramaPayRank[]).map((item: DramaPayRank, index: number): PayRank => {
      return { rank: index + 1, title: item.dramaTitle, subTitle: `(${item.director})`, value: item.totalAmount };
    });
  } else {
    return (list as UserPayRank[]).map((item: UserPayRank, index: number): PayRank => {
      return { rank: index + 1, title: item.nickname, subTitle: `(${item.username})`, value: item.payAmount };
    });
  }
}
